package com.kedacom.freedomLabel.view.fragment.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.UserCache;
import com.kedacom.freedomLabel.databinding.FragmentUserInformationBinding;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.freedomLabel.view.widget.NoteItemView;
import com.kedacom.freedomLabel.viewmodel.UserInformationViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.util.Utils;

import java.util.ArrayList;
import java.util.List;


@ContentView(R.layout.fragment_user_information)
@ViewModel(UserInformationViewModel.class)
public class UserInformationFragment extends ASettingFragment<FragmentUserInformationBinding, UserInformationViewModel> {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        LLog.debug("UserInformationFragment - onCreateView " + this.toString());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LLog.debug("UserInformationFragment - onDestroyView " + this.toString());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LLog.debug("UserInformationFragment - onViewCreated " + this.toString());
        initView();
    }

    private void initView() {
        mBinding.printParamCompany.setContentText(UserCache.companyName);
        mBinding.printParamAddress.setContentText(UserCache.companyAddress);
        mBinding.printParamTel.setContentText(UserCache.companyTel);
        mBinding.printParamCompanySite.setContentText(UserCache.internetSite);
        mBinding.printParamCompanySlogan.setContentText(UserCache.slogan);
    }

    @Override
    public void saveData() {
        int childCount = mBinding.printParamParent.getChildCount();
        List<Config> configList = new ArrayList<>(childCount);
        for (int index = 0; index < childCount; index++) {
            View childAt = mBinding.printParamParent.getChildAt(index);
            if (childAt instanceof NoteItemView) {
                NoteItemView itemView = (NoteItemView) childAt;
                String configKey = itemView.getTag().toString();
                String configValue = itemView.getContentText();
                configList.add(addParameter(configKey, configValue));
            }
        }
        mViewModel.saveUserInformation(configList);
    }

    @Override
    public String getFragmentTitle() {
        return Utils.getApp().getString(R.string.setting_frag_name_user);
    }

    private Config addParameter(String key, String value) {
        return new Config(key, value);
    }
}